<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>线程池设计与实现原理</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Helvetica Neue', Arial, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #2563eb 0%, #1e40af 100%);
        }
        .code-block {
            background: #1e293b;
            border-left: 4px solid #3b82f6;
        }
        .prose a {
            color: #3b82f6;
            text-decoration: none;
            transition: all 0.2s ease;
        }
        .prose a:hover {
            color: #2563eb;
            text-decoration: underline;
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            background: linear-gradient(90deg, rgba(59, 130, 246, 0.1) 0%, rgba(59, 130, 246, 0) 100%);
            border-left: 3px solid #3b82f6;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-5xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-4 font-serif">线程池设计与实现原理</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8 max-w-3xl mx-auto">高效管理并发任务的艺术</p>
            <div class="flex justify-center space-x-4">
                <a href="#why" class="px-6 py-3 bg-white text-blue-600 font-medium rounded-lg hover:bg-opacity-90 transition">为什么使用线程池</a>
                <a href="#how" class="px-6 py-3 border-2 border-white text-white font-medium rounded-lg hover:bg-white hover:bg-opacity-10 transition">核心原理</a>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Why Thread Pool Section -->
        <section id="why" class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-question text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">为什么要使用线程池？</h2>
            </div>
            
            <div class="prose max-w-none text-gray-700">
                <div class="bg-white rounded-xl shadow-sm p-6 mb-6">
                    <p class="text-lg">线程是稀缺资源，每创建一个线程都需要消耗系统资源。如果不对创建线程做控制，系统的资源就会被耗尽，最终导致服务器卡顿或宕机。线程池通过对线程创建的控制，达到资源管理的效果。</p>
                </div>
                
                <div class="grid md:grid-cols-3 gap-6 mb-8">
                    <div class="card bg-white rounded-xl shadow-sm p-6">
                        <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mb-4">
                            <i class="fas fa-bolt text-blue-600"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">降低资源消耗</h3>
                        <p class="text-gray-600">通过重复利用已创建的线程降低线程创建和销毁造成的消耗</p>
                    </div>
                    <div class="card bg-white rounded-xl shadow-sm p-6">
                        <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center mb-4">
                            <i class="fas fa-tachometer-alt text-green-600"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">提高响应速度</h3>
                        <p class="text-gray-600">当任务到达时，可以不需要等待线程创建就能立即执行</p>
                    </div>
                    <div class="card bg-white rounded-xl shadow-sm p-6">
                        <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mb-4">
                            <i class="fas fa-sliders-h text-purple-600"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">提高可管理性</h3>
                        <p class="text-gray-600">线程是稀缺资源，使用线程池可以进行统一的分配，监控和调优</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Basic Usage Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-code text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">线程池基本使用</h2>
            </div>
            
            <div class="prose max-w-none">
                <div class="code-block rounded-lg overflow-hidden mb-6">
                    <pre class="text-gray-200 p-6 overflow-x-auto"><code class="language-java">//获取当前机器的核数    
private static Integer code = Runtime.getRuntime().availableProcessors();

static {
    threadPoolExecutor = new ThreadPoolExecutor(
        code, // 核心线程数(线程池中始稳定的线程最小数量)
        code * 2, // 最大线程数量
        3, // 空闲线程时间
        TimeUnit.SECONDS, // 时间单位
        new LinkedBlockingQueue<>(500), // 队列
        Executors.defaultThreadFactory(), // 线程创建工厂
        new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
    );
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Core Parameters Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-cog text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">线程池7大核心参数</h2>
            </div>
            
            <div class="prose max-w-none">
                <div class="code-block rounded-lg overflow-hidden mb-6">
                    <pre class="text-gray-200 p-6 overflow-x-auto"><code class="language-java">public ThreadPoolExecutor(int corePoolSize,
    int maximumPoolSize,
    long keepAliveTime,
    TimeUnit unit,
    BlockingQueue&lt;Runnable&gt; workQueue,
    ThreadFactory threadFactory,
    RejectedExecutionHandler handler)</code></pre>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-circle-notch text-blue-500 mr-2 text-sm"></i> 核心参数详解
                        </h3>
                        <ul class="space-y-3">
                            <li><span class="font-bold">corePoolSize:</span> 线程池核心线程数量，核心线程不会被回收，即使没有任务执行，也会保持空闲状态</li>
                            <li><span class="font-bold">maximumPoolSize:</span> 池允许最大的线程数，当线程数量达到corePoolSize，且workQueue队列塞满任务后，继续创建线程</li>
                            <li><span class="font-bold">keepAliveTime:</span> 超过corePoolSize之后的"临时线程"的存活时间</li>
                            <li><span class="font-bold">unit:</span> keepAliveTime的时间单位</li>
                        </ul>
                    </div>
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-circle-notch text-blue-500 mr-2 text-sm"></i> 高级配置
                        </h3>
                        <ul class="space-y-3">
                            <li><span class="font-bold">workQueue:</span> 当前线程数超过corePoolSize时，新的任务会处在等待状态，并存在workQueue中</li>
                            <li><span class="font-bold">threadFactory:</span> 创建线程的工厂类，通常自定义threadFactory设置线程的名称</li>
                            <li><span class="font-bold">handler:</span> 线程池执行拒绝策略，当线数量达到maximumPoolSize，且workQueue已满时调用</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Rejection Policy Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-ban text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">拒绝策略</h2>
            </div>
            
            <div class="prose max-w-none">
                <div class="grid md:grid-cols-4 gap-4">
                    <div class="card bg-white rounded-xl shadow-sm p-6 border-t-4 border-red-500">
                        <h3 class="text-lg font-bold mb-2">AbortPolicy</h3>
                        <p class="text-gray-600 text-sm">为线程池默认的拒绝策略，该策略直接抛异常处理</p>
                    </div>
                    <div class="card bg-white rounded-xl shadow-sm p-6 border-t-4 border-yellow-500">
                        <h3 class="text-lg font-bold mb-2">DiscardPolicy</h3>
                        <p class="text-gray-600 text-sm">直接抛弃不处理</p>
                    </div>
                    <div class="card bg-white rounded-xl shadow-sm p-6 border-t-4 border-green-500">
                        <h3 class="text-lg font-bold mb-2">DiscardOldestPolicy</h3>
                        <p class="text-gray-600 text-sm">丢弃队列中最老的任务</p>
                    </div>
                    <div class="card bg-white rounded-xl shadow-sm p-6 border-t-4 border-blue-500">
                        <h3 class="text-lg font-bold mb-2">CallerRunsPolicy</h3>
                        <p class="text-gray-600 text-sm">将任务分配给当前调用execute方法线程来处理(同步执行)</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Work Queue Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-stream text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">线程池工作队列</h2>
            </div>
            
            <div class="prose max-w-none">
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-cube text-blue-500 mr-2"></i> ArrayBlockingQueue
                        </h3>
                        <p class="text-gray-600">基于数组结构的有界阻塞队列，按FIFO原则排序元素。生产者和消费者共用同一个锁对象，无法真正并行运行。</p>
                    </div>
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-link text-blue-500 mr-2"></i> LinkedBlockingQueue
                        </h3>
                        <p class="text-gray-600">基于链表结构的有界阻塞队列，按FIFO排序元素，吞吐量通常高于ArrayBlockingQueue，默认容量为Integer.MAX_VALUE。</p>
                    </div>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-sort-amount-up text-blue-500 mr-2"></i> PriorityBlockingQueue
                        </h3>
                        <p class="text-gray-600">有优先级的无界阻塞队列，优先级通过参数Comparator实现。通过构造方法参数设置容量和比较器。</p>
                    </div>
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-exchange-alt text-blue-500 mr-2"></i> SynchronousQueue
                        </h3>
                        <p class="text-gray-600">一个没有容量的队列，只要有请求到来，就必须要找到一条工作线程处理他，否则会创建新线程。</p>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-sm p-6 mb-8">
                    <h3 class="text-xl font-bold mb-4">LinkedBlockingQueue和ArrayBlockingQueue区别</h3>
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">对比项</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ArrayBlockingQueue</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">LinkedBlockingQueue</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">队列大小</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">有界，初始化必须指定大小</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">可以有界也可以无界(默认Integer.MAX_VALUE)</td>
                                </tr>
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">存储结构</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">数组</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">链表(Node节点)</td>
                                </tr>
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">GC影响</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">不产生额外对象</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">会产生Node对象，可能影响GC</td>
                                </tr>
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">锁机制</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">添加和移除使用同一把锁</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">添加(putLock)和移除(takeLock)使用不同锁</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </section>

        <!-- Thread Pool Config Section -->
        <section class="mb-16" id="how">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-sliders-h text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">线程池核心数的设置</h2>
            </div>
            
            <div class="prose max-w-none">
                <div class="bg-white rounded-xl shadow-sm p-6 mb-8">
                    <p class="text-lg">线程数的设置的最主要的目的是为了充分并合理地使用 CPU 和内存等资源，从而最大限度地提高程序的性能。</p>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-microchip text-blue-500 mr-2"></i> CPU密集型
                        </h3>
                        <p class="text-gray-600 mb-4">CPU密集型也可以叫做计算密集型，指的是CPU有许多操作计算要进行处理，这时候CPU加载很高。</p>
                        <div class="highlight p-4 rounded-lg">
                            <p class="text-sm font-medium">核心线程数 = CPU核心数 + 1</p>
                        </div>
                    </div>
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-hdd text-blue-500 mr-2"></i> IO密集型
                        </h3>
                        <p class="text-gray-600 mb-4">IO密集型跟CPU密集型正好相反，系统运行时CPU利用率不高，都是IO读写的操作。</p>
                        <div class="highlight p-4 rounded-lg">
                            <p class="text-sm font-medium">核心线程数 = CPU核心数 × 2</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Thread Pool Workflow Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-project-diagram text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">ThreadPoolExecutor 运行流程</h2>
            </div>
            
            <div class="prose max-w-none">
                <div class="bg-white rounded-xl shadow-sm p-6 mb-8">
                    <div class="mermaid">
                        graph TD
                            A[提交任务] --> B{核心线程是否已满?}
                            B -->|否| C[创建核心线程执行任务]
                            B -->|是| D{队列是否已满?}
                            D -->|否| E[任务加入队列等待执行]
                            D -->|是| F{线程数是否达到最大值?}
                            F -->|否| G[创建非核心线程执行任务]
                            F -->|是| H[执行拒绝策略]
                    </div>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4">线程池运行状态</h3>
                        <ul class="space-y-3">
                            <li><span class="font-bold">RUNNING:</span> 能接受新提交的任务，也能处理阻塞队列中的任务</li>
                            <li><span class="font-bold">SHUTDOWN:</span> 不再接受新提交的任务，但可以继续处理阻塞队列中已保存的任务</li>
                            <li><span class="font-bold">STOP:</span> 不再接受新提交的任务，同时抛弃阻塞队列里的所有任务并中断所有正在执行任务</li>
                            <li><span class="font-bold">TIDYING:</span> 所有任务都执行完毕，workerCount有效线程数为0</li>
                            <li><span class="font-bold">TERMINATED:</span> 终止状态</li>
                        </ul>
                    </div>
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4">任务调度逻辑</h3>
                        <ol class="space-y-3 list-decimal pl-5">
                            <li>检查线程池运行状态，如果不是RUNNING则拒绝</li>
                            <li>如果workerCount < corePoolSize，创建并启动新线程</li>
                            <li>如果workerCount >= corePoolSize且队列未满，将任务加入队列</li>
                            <li>如果队列已满且workerCount < maximumPoolSize，创建非核心线程</li>
                            <li>如果workerCount >= maximumPoolSize且队列已满，执行拒绝策略</li>
                        </ol>
                    </div>
                </div>
            </div>
        </section>

        <!-- Common Thread Pools Section -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-layer-group text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">常见线程池类型</h2>
            </div>
            
            <div class="prose max-w-none">
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-th-large text-blue-500 mr-2"></i> FixedThreadPool
                        </h3>
                        <p class="text-gray-600 mb-4">固定大小的线程池，核心线程数和最大线程数相同，使用LinkedBlockingQueue作为工作队列。</p>
                        <div class="code-block rounded-lg overflow-hidden">
                            <pre class="text-gray-200 p-4 overflow-x-auto text-sm"><code class="language-java">Executors.newFixedThreadPool(int nThreads)</code></pre>
                        </div>
                    </div>
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-infinity text-blue-500 mr-2"></i> CachedThreadPool
                        </h3>
                        <p class="text-gray-600 mb-4">可缓存的线程池，核心线程数为0，最大线程数为Integer.MAX_VALUE，使用SynchronousQueue。</p>
                        <div class="code-block rounded-lg overflow-hidden">
                            <pre class="text-gray-200 p-4 overflow-x-auto text-sm"><code class="language-java">Executors.newCachedThreadPool()</code></pre>
                        </div>
                    </div>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-clock text-blue-500 mr-2"></i> ScheduledThreadPool
                        </h3>
                        <p class="text-gray-600 mb-4">定时线程池，用于执行定时或周期性的任务，使用DelayedWorkQueue。</p>
                        <div class="code-block rounded-lg overflow-hidden">
                            <pre class="text-gray-200 p-4 overflow-x-auto text-sm"><code class="language-java">Executors.newScheduledThreadPool(int corePoolSize)</code></pre>
                        </div>
                    </div>
                    <div class="bg-white rounded-xl shadow-sm p-6">
                        <h3 class="text-xl font-bold mb-4 flex items-center">
                            <i class="fas fa-user text-blue-500 mr-2"></i> SingleThreadExecutor
                        </h3>
                        <p class="text-gray-600 mb-4">单一线程池，核心线程数和最大线程数都为1，使用LinkedBlockingQueue。</p>
                        <div class="code-block rounded-lg overflow-hidden">
                            <pre class="text-gray-200 p-4 overflow-x-auto text-sm"><code class="language-java">Executors.newSingleThreadExecutor()</code></pre>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-sm p-6">
                    <h3 class="text-xl font-bold mb-4 flex items-center">
                        <i class="fas fa-code-branch text-blue-500 mr-2"></i> ForkJoinPool
                    </h3>
                    <p class="text-gray-600">ForkJoinPool线程池最大的特点就是分叉(fork)合并(join)，将一个大任务拆分成多个小任务，并行执行，再结合工作窃取模式(worksteal)提高整体的执行效率，充分利用CPU资源。</p>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section>
            <div class="flex items-center mb-8">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                    <i class="fas fa-check-circle text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold font-serif">总结</h2>
            </div>
            
            <div class="prose max-w-none">
                <div class="bg-white rounded-xl shadow-sm p-6">
                    <ol class="space-y-4 list-decimal pl-5">
                        <li>线程池刚创建时，里面没有一个线程。任务队列是作为参数传进来的。不过，就算队列里面有任务，线程池也不会马上执行它们。</li>
                        <li>
                            当调用execute()方法添加一个任务时，线程池会做如下判断：
                            <ul class="list-disc pl-5 mt-2 space-y-2">
                                <li>如果正在运行的线程数量小于corePoolSize，那么马上创建线程运行这个任务</li>
                                <li>如果正在运行的线程数量大于或等于corePoolSize，那么将这个任务放入队列</li>
                                <li>如果这时候队列满了，而且正在运行的线程数量小于maximumPoolSize，那么还是要创建非核心线程立刻运行这个任务</li>
                                <li>如果队列满了，而且正在运行的线程数量大于或等于maximumPoolSize，那么线程池会抛出异常RejectExecutionException</li>
                            </ul>
                        </li>
                        <li>当一个线程完成任务时，它会从队列中取下一个任务来执行</li>
                        <li>当一个线程无事可做，超过keepAliveTime时，如果当前运行的线程数大于corePoolSize，那么这个线程就被停掉。所以线程池的所有任务完成后，它最终会收缩到corePoolSize的大小</li>
                    </ol>
                </div>
            </div>
        </section>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Smooth scrolling for anchor links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>